package com.fenghongzhang.day016;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;

import android.Manifest;
import android.content.ComponentName;
import android.content.Intent;
import android.content.ServiceConnection;
import android.content.pm.PackageManager;
import android.os.Binder;
import android.os.Build;
import android.os.Bundle;
import android.os.IBinder;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ListView;
import android.widget.PopupWindow;
import android.widget.SeekBar;
import android.widget.TextView;
import android.widget.Toast;

import java.sql.Time;
import java.util.List;
import java.util.Timer;
import java.util.TimerTask;

public class MainActivity extends AppCompatActivity {
    private ListView lv;
    private SeekBar seekbar;
    private Button pause;
    private Button stop;
    private Button restart;
    private Button pre;
    private static final String TAG = "MainActivity";
    private Button next;

    private List<Music> music;
    private ServiceConnection serviceConnection;
    private MusicService musicService;
    private Button playMode;

    private Timer timer;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        lv = (ListView) findViewById(R.id.lv);
        seekbar = (SeekBar) findViewById(R.id.seekbar);
        pause = (Button) findViewById(R.id.pause);
        stop = (Button) findViewById(R.id.stop);
        restart = (Button) findViewById(R.id.restart);
        pre = (Button) findViewById(R.id.pre);
        next = (Button) findViewById(R.id.next);

        playMode = (Button) findViewById(R.id.playMode);


        /**
         *  模式
         *  1,弹出一个popwin
         *  2,三个选择 textView
         *  3,设置不同的值
         */
        playMode.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                final PopupWindow popupWindow = new PopupWindow();
                popupWindow.setWidth(ViewGroup.LayoutParams.WRAP_CONTENT);
                popupWindow.setHeight(300);
                View inflate = LayoutInflater.from(MainActivity.this).inflate(R.layout.pop_layout, null);
                popupWindow.setContentView(inflate);

                TextView textView = inflate.findViewById(R.id.pop_order);
                textView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        popupWindow.dismiss();
                        musicService.setPlayMode(MusicUtil.MODE_ORDER);
                        Toast.makeText(MainActivity.this, "顺序播放", Toast.LENGTH_SHORT).show();
                    }
                });

                TextView random = inflate.findViewById(R.id.pop_random);
                random.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        musicService.setPlayMode(MusicUtil.MODE_RANDOM);
                        popupWindow.dismiss();
                        Toast.makeText(MainActivity.this, "随机播放", Toast.LENGTH_SHORT).show();
                    }
                });

                TextView single = inflate.findViewById(R.id.pop_single);
                single.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        popupWindow.dismiss();
                        musicService.setPlayMode(MusicUtil.MODE_SINGLE);
                        Toast.makeText(MainActivity.this, "单曲播放", Toast.LENGTH_SHORT).show();
                    }
                });

                popupWindow.setOutsideTouchable(true);
                popupWindow.showAsDropDown(playMode,0,-400);
            }
        });


        next.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                musicService.next();
            }
        });

        pre.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                musicService.pre();
            }
        });

        stop.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                musicService.bbbbbb();
            }
        });

        restart.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                musicService.restart();
            }
        });

        pause.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                musicService.pause();
            }
        });

        //0权限
        if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.M){
            requestPermissions(new String[]{Manifest.permission.READ_EXTERNAL_STORAGE},100);
        }

        Intent intent = new Intent(this, MusicService.class);
        startService(intent);

        serviceConnection = new ServiceConnection() {
            @Override
            public void onServiceConnected(ComponentName name, IBinder service) {
                 MusicService.MyBinder binder = (MusicService.MyBinder) service;
                 //通过binder对象得到一个Service
                musicService = binder.getService();
            }

            @Override
            public void onServiceDisconnected(ComponentName name) {

            }
        };
        bindService(intent,serviceConnection,BIND_AUTO_CREATE);

        seekbar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
            @Override
            public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
                if(fromUser){
                    // a - > s
                    musicService.setProgress(progress);
                }
            }

            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {

            }

            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {

            }
        });

        //listView的点击事件
        lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                musicService.play(position);
                Log.i(TAG, "onItemClick: "+position);
                if(timer != null){
                    timer.cancel();
                }

                //定时器
                timer = new Timer();
                timer.schedule(new TimerTask() {
                    @Override
                    public void run() {
                        seekbar.setMax(musicService.getMax()); //0
                        //每秒更新的
                        seekbar.setProgress(musicService.getCurrentPosition());//0
                    }
                },2000,1000);
            }
        });

    }

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        if(requestCode == 100 && grantResults[0] == PackageManager.PERMISSION_GRANTED){
            //1,内容->数据源

            music = MusicUtil.getMusic(this);
            MusicAdapter musicAdapter = new MusicAdapter(this,music);
            lv.setAdapter(musicAdapter);

        }else{
//            finish();
            Toast.makeText(this, "没权限", Toast.LENGTH_SHORT).show();
        }
    }
}
